package be.phl.obstacleapp;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import be.phl.model.DBAdapterPatient;
import android.app.Fragment;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class PersonalDataFragment extends Fragment {

	private static final String TAG = "PersonalDataFragment";

	Button btnSave;
	EditText txtLength, txtWeight;
	ToggleButton tbtnGender;
	DatePicker DateOfBirth;
	Spinner MaritalStatus;
	TextView txtD, txtL, txtW, txtG, txtM;
	private int PatientId;
	private String gender;
	ArrayList<String> maritalstatuses = new ArrayList<String>();

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstances) {
		return inflater.inflate(R.layout.fragment_personal_data, container,
				false);
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		txtLength = (EditText) getActivity().findViewById(R.id.txtLength);
		txtWeight = (EditText) getActivity().findViewById(R.id.txtWeight);
		tbtnGender = (ToggleButton) getActivity().findViewById(R.id.tbtnGender);
		DateOfBirth = (DatePicker) getActivity().findViewById(R.id.DateOfBirth);
		MaritalStatus = (Spinner) getActivity()
				.findViewById(R.id.MaritalStatus);
		btnSave = (Button) getActivity().findViewById(R.id.btnSave);
		txtD = (TextView) getActivity().findViewById(R.id.txtD);
		txtL = (TextView) getActivity().findViewById(R.id.txtL);
		txtW = (TextView) getActivity().findViewById(R.id.txtW);
		txtG = (TextView) getActivity().findViewById(R.id.txtG);
		txtM = (TextView) getActivity().findViewById(R.id.txtM);

		Typeface tf = Typeface.createFromAsset(getActivity().getAssets(),
				"com.ttf");

		txtD.setTypeface(tf, Typeface.BOLD);
		txtL.setTypeface(tf, Typeface.BOLD);
		txtW.setTypeface(tf, Typeface.BOLD);
		txtG.setTypeface(tf, Typeface.BOLD);
		txtM.setTypeface(tf, Typeface.BOLD);
		btnSave.setTypeface(tf, Typeface.BOLD);

		DBAdapterPatient db = new DBAdapterPatient(getActivity());

		db.open();
		Cursor ms = db.getAllMaritalStatus();

		while (ms.moveToNext()) {
			maritalstatuses.add(ms.getString(1));
		}
		db.close();

		ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
				getActivity(), android.R.layout.simple_spinner_item,
				maritalstatuses);
		spinnerArrayAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The
																							// drop
																							// down
																							// vieww
		MaritalStatus.setAdapter(spinnerArrayAdapter);

		btnSave.setOnClickListener(btnOnClickListener);

		PatientId = (int) getActivity().getIntent().getIntExtra("PatientId", 0);
		db.open();
		Cursor p = db.getPersonalData(PatientId);

		while (p.moveToNext()) {
			txtLength.setText(p.getString(4));
			txtWeight.setText(p.getString(3));

			if (p.getString(1).equals("M")) {
				tbtnGender.setChecked(true);
			} else {
				tbtnGender.setChecked(false);
			}

			Date date = null;
			try {
				Log.i(TAG, "Patient day of bith: " + p.getString(2));
				date = MainData.getDateformat().parse(p.getString(2));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			DateOfBirth.getCalendarView().setDate(date.getTime());

			MaritalStatus.setSelection(p.getInt(5) - 1);

		}
		db.close();
	}

	Button.OnClickListener btnOnClickListener = new Button.OnClickListener() {
		@Override
		public void onClick(View v) {
			Pattern p = Pattern.compile("([0-9]*)");

			Matcher length = p.matcher(txtLength.getText().toString());
			Matcher weight = p.matcher(txtWeight.getText().toString());
			if (txtLength.getText().toString().equals("")) {
				txtLength.setError("Mag niet leeg zijn");
				txtLength.requestFocus();
			} else if (!length.matches()) {
				txtLength.setError("Mag enkel uit nummers bestaan");
				txtLength.requestFocus();
			} else if (txtWeight.getText().toString().equals("")) {
				txtWeight.setError("Mag niet leeg zijn");
				txtWeight.requestFocus();
			} else if (!weight.matches()) {
				txtLength.setError("Mag enkel uit nummers bestaan");
				txtLength.requestFocus();
			} else {

				PatientId = (int) getActivity().getIntent().getIntExtra(
						"PatientId", 0);
				DBAdapterPatient db = new DBAdapterPatient(getActivity());
				db.open();

				Date date = new Date();
				date.setTime(DateOfBirth.getCalendarView().getDate());

				if (tbtnGender.isChecked()) {
					setGender("M");
				} else {
					setGender("V");
				}

				if (db.updatePersonalData(PatientId, MainData.getDateformat()
						.format(date), txtLength.getText().toString(),
						txtWeight.getText().toString(), getGender(),
						MaritalStatus.getSelectedItemPosition() + 1) > 0) {
					Context context = getActivity().getApplicationContext();
					CharSequence text = "Aanpassen succesvol!";
					int duration = Toast.LENGTH_SHORT;

					Toast toast = Toast.makeText(context, text, duration);
					toast.show();
				} else {
					Context context = getActivity().getApplicationContext();
					CharSequence text = "Aanpassen mislukt!";
					int duration = Toast.LENGTH_SHORT;

					Toast toast = Toast.makeText(context, text, duration);
					toast.show();
				}
				db.close();
			}
		}
	};
}
